.netCHARTING v4.2 Documentation Send comments on this topic.
Shortcuts and Efficient Code

Glossary Item Box

Introduction

The .netCHARTING API has been designed to allow users to dramatically cut down on the amount of code necessary to perform common tasks. This topic will demonstrate how many multi-task operations can be done very easily, usually with a single line of code. If you spend much of your time working with charts please read on.

 

Defaults

One of the most time saving features are the property defaults. These allow you to set properties of all similar objects with a single line of code. For example, a chart can have a large number of series and elements. To quickly set any of those series or element properties a single line of code can be used as shown here:

[C#]

Chart.DefaultSeries.Type = SeriesType.Line;
Chart.DefaultElement.ShowValue = true;

[Visual Basic]

Chart.DefaultSeries.Type = SeriesType.Line
Chart.DefaultElement.ShowValue = True

Not every single objects of the same type has to bet set using defaults. They can also be set selectively for specific groups. For example, all elements of only a particular series can be set without changing element in other series using the following code:

[C#]

mySeries.DefaultElement.ShowValue = true;

[Visual Basic]

mySeries.DefaultElement.ShowValue = True

This is a list of default properties that can be used to quickly specify settings:

 

Common Collection Construction

Many collection objects in .netCHARTING have methods that make it much faster and easier to create them. Let's take a look at some of these features using the ChartAreaCollection object.

Constructor
public ChartAreaCollection(params ChartArea[] chartAreaList)

This constructor allows you to create a collection with a number of chart areas using a single line of code:

[C#]

ChartAreaCollection cac = new ChartAreaCollection(ca1, ca2, ca3, ca4);

[Visual Basic]

Dim cac As New ChartAreaCollection(ca1, ca2, ca3, ca4)

Add Methods

public void Add(params ChartArea[] chartAreaList)

Objects can be added to existing collections in the same manner as the constructor

[C#]

Chart.ExtraChartAreas.Add(ca1, ca2, ca3, ca4);

[Visual Basic]

Chart.ExtraChartAreas.Add(ca1, ca2, ca3, ca4)

public void Add(ChartAreaCollection chartAreaCollection)

Adding a collection of chart areas to another chart area collection can be done like so:

[C#]

Chart.ExtraChartAreas.Add(myChartAreaCollection);

[Visual Basic]

Chart.ExtraChartAreas.Add(myChartAreaCollection)

Almost all collections contain these shortcut members.

 

Collection Type Specific Construction & Features

Some collections also have shortcut constructors that are specific to the type of data they represent.

AxisTickCollection

public static AxisTickCollection FromValues(params DateTime[] list)
public static AxisTickCollection FromValues(params double[] list)

This static constructor allows you to add axis ticks with specific values using a single line of code.

[C#]

Chart.YAxis.ClearValues = true;
Chart.YAxis.ExtraTicks.Add(AxisTickCollection.FromValues(1,2,3,4,5,6);

[Visual Basic]

Chart.YAxis.ClearValues = True
Chart.YAxis.ExtraTicks.Add(AxisTickCollection.FromValues(1,2,3,4,5,6)

Notice: The ExtraTicks.Add method is taking an AxisTickCollection.


Series

The Series object is not a collection. The elements are contained in the Series.Elements ElementCollection property. There are, however, features available that allow managing a series' elements simpler. Series can be populated with elements quickly using the FromYValues constructor.

public static Series FromYValues(params double[] yValues)

[C#]

Series s = Series.FromYValues(1,5,6,34,2);

[Visual Basic]

Dim s As Series = Series.FromYValues(1, 5, 6, 34, 2)


The following method allows constructing a series from a list of elements.

public Series(string name, params Element[] elementList)

[C#]

Series s = new Series("mySeries", element1, element2,element3, element4);

[Visual Basic]

Dim s As New Series("mySeries", element1, element2,element3, element4)

There is one more series feature worth noting. As mentioned above, the series is not a collection. Nevertheless, its elements can be accessed easily using the series' indexer:

[C#]

Element myElement = mySeries[1];
// Equivalent to:
Element myElement = mySeries.Elements[1];

[Visual Basic]

Dim myElement As Element = mySeries(1)
' Equivalent to:
Dim myElement As Element = mySeries.Elements(1)

Using these collection object shortcuts will allow you to write code much faster and spend more time focusing on your application logic.